import {reactive, onMounted, onBeforeUnmount} from "vue";
import {getCountry, getCity, getPurchaseUserList, getCurrencyList} from "@/api/common"

/**
 * 国家
 */
export function getCountryList(form) {
  getCountry({}).then(res => {
    let countryList = res.data.countryList
    countryList.forEach(item => {
      item['label'] = item.countryRegion
      item['value'] = item.countryRegion
    })
    form.option = countryList
  })
}
/**
 * 城市
 * **/
export function getCityList(country = "中国", form) {
  let params = {
    countryRegion: country
  }
  getCity(params).then(res => {
    let cityList = res.data.cityList;
    cityList.forEach(item => {
      item['label'] = item.city == null ? item.countryRegion : item.city
      item['value'] = item.city == null ? item.countryRegion : item.city
    })
    form.option = cityList
  })
}

/**
 * 模糊获取采购人 销售人
 * **/
export function getUserListByRole(params, form, callback) {
  getPurchaseUserList(params).then((res) => {
    let purchaseUserList = res.data.purchaseUserList
    purchaseUserList.forEach((item) => {
      item.value = item.nickName
      item.label = item.nickName
    })
    form.option = purchaseUserList
    callback && callback(purchaseUserList);
  })
}

/**
 * 数据字典组装
 * **/
export function setDictionaryLabelAndVal(arr) {
  arr.forEach(item => {
    item['label'] = item.dictValueName
    item['value'] = item.dictValueName
  })
  return arr
}

/**
 * 币种
 * **/
export function getCurrencyListHand(params, form, callback) {
  getCurrencyList(params).then((res) => {
    let currencyList = res.data.currencyList
    currencyList.forEach((item) => {
      item.value = item.currencyName
      item.label = item.currencyName
    })
    form.option = currencyList
    callback && callback(currencyList);
  })
}

/**
* 查找对象中的key
* */
export function getObjectKey(formData, prop, index = 1, columnIndex = 0) {
  let formProp = null, formChildrenProp;
  for (let key in formData) {
    if (key == prop[index - 1]) {
      if (index == prop.length) {
        formProp = formData[prop[index - 1]]
      } else {
        formChildrenProp = formData[prop[index - 1]]
      }
      break
    }
  }
  if (formProp == null) {
    return getObjectKey(formChildrenProp[columnIndex], prop, index + 1)
  } else {
    return formProp
  }
}






















